<template>
	<view class="container-gray">
		<!-- 		<view class="status_bar">
    </view>-->
		<view>
			<view class="fixed-box">
				<view class="flex-box" style="border-top: 1px solid #EDEDED;padding: 20upx 30upx;flex: display;justify-content: space-between;font-size: 16px;">
					<view @click="allCourse">全部课程
						<image style="width: 23upx;height: 15upx;margin-left: 6px;" src="../../static/img/xxjt.png" />
					</view>
					<view v-if="showIng" @click="newest">
						{{stateName}}
						<image style="width: 23upx;height: 15upx;margin-left: 6px;" src="../../static/img/bluesx.png" />
					</view>
					<view v-else @click="newest">
						{{stateName}}
						<image style="width: 23upx;height: 15upx;margin-left: 6px;" src="../../static/img/xxjt.png" />
					</view>
				</view>
				<view v-if="showIng" @click="isShowIng" style="background: rgba(0,0,0,0.5);position: fixed;z-index: 1001;width: 100%;height: 100%;display: flex;">
					<view style="text-align: center;width: 100%;height: 100px;background: #FFFFFF;">
						<view @click="coursestate('最新',0)" style="padding: 15upx 0; line-height: 30px;">最新</view>
						<view @click="coursestate('最热',1)" style="padding: 15upx 0;">最热</view>
					</view>
				</view>
			</view>
		</view>

		<view style="margin-top: 100upx;padding: 10upx 20upx;">
			<view v-if="dataList.length">
				<view v-for="(item,index) in dataList" :key="index">
					<view class="flex-box course-info-box" @click="goStudy(item)">
						<image class="info-img" :src="item.imgUrl" options mode="aspectFill" />
						<view style="margin-left: 10upx;flex:1;">
							<view class="info-title" style="height: 120upx;">{{item.title}}</view>
							<view style="font-size: 20upx;color: #6A6A6A;" v-if="item.status == 0">未开始</view>
							<view style="font-size: 20upx;color: #6A6A6A;" v-if="item.status == 1">未完成</view>
							<view style="font-size: 20upx;color: #6A6A6A;" v-if="item.status == 2">已完成</view>
						</view>
					</view>
				</view>
				<uni-load-more :status="loadingType" :contentText="contentText"></uni-load-more>
			</view>
			<view v-else class="container-tips">暂无相关内容</view>
		</view>
		<!-- 弹框 -->
		<view></view>
	</view>
</template>

<script>
	import {
		post_md5,
		request_integral
	} from '../../common/request'
	import xflSelect from '@/components/xfl-select/xfl-select.vue'
	export default {
		data() {
			return {
				list: [
					//要展示的数据
					'最新',
					'最热'
				],
				showIng: false,
				title: '',
				logo: '',
				stateName: '最新',
				dataList: [],
				page: 1,
				cateId: '',
				idx: 1,
				courseId: null,
				cateList: [],
				refreshText: '下拉可以刷新',
				loadingType: 'more', //定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				}
			}
		},
		components: {
			xflSelect
		},
		onLoad(options) {
			this.cateId = options.id
			// this.getSysInfo()
			this.getCourseList(0)
		},
		onShow() {
			if (this.courseId != null) {
				this.getcoursetype()
			}
			this.loadingType = 'more'
			// if (this.courseId != null) {
			// 	const params = {
			// 		courseId: this.courseId,
			// 		userId: uni.getStorageSync('userId')
			// 	}
			// 	post_md5('/train-app/courseapp/updateusercoursestatus', params).then(
			// 		res => {
			// 			this.dataList.forEach(x => {
			// 				if (x.id == res.data.courseId) {
			// 					x.status = res.data.status
			// 				}
			// 			})
			// 			console.log(this.dataList)
			// 			// if (res.code == 200) {
			// 			// 	uni.navigateBack()
			// 			// }
			// 		}
			// 	)
			// }
			this.getCourseList(1)
		},
		onPullDownRefresh: function() {
			//下拉刷新的时候请求一次数据
			this.page = 1
			this.dataList = []
			this.loadingType = 'more'
			this.getCourseList(0)
		},
		onReachBottom: function() {
			// this.page--
			this.getCourseList()
		},
		onNavigationBarButtonTap(Object) {
			this.goSearch()
		},
		methods: {
			coursestate(stateName, val) {
				this.stateName = stateName
				this.dataList = []
				this.page = 1
				this.idx = val
				this.getCourseList(this.idx)
			},
			isShowIng() {
				this.showIng = false
			},
			getcoursetype() {
				const params = {
					courseId: this.courseId,
					userId: uni.getStorageSync('userId')
				}
				post_md5('/train-app/courseapp/coursestatus', params).then(
					res => {
						if (res.data == 2) request_integral('course', this.courseId)
						this.dataList.forEach(x => {
							if (x.id == this.courseId) {
								x.status = res.data
							}
						})
						console.log(res)
					}
				)
			},
			newest() {
				this.showIng = true
			},
			getCourseList(idx) {
				uni.showNavigationBarLoading() //显示加载动画
				const params = {
					page: this.page,
					userId: uni.getStorageSync('userId'),
					token: uni.getStorageSync('token'),
					orderType: parseInt(idx)
				}
				if (this.cateId) {
					params.cateId = this.cateId
				}
				post_md5('/train-app/courseapp/getcourselist', params).then(res => {
					console.log(res)
					uni.stopPullDownRefresh()
					this.dataList = res.data.list
					if (this.dataList.length == res.data.totalCount) {
						this.loadingType = 'noMore'
					} else {
						// uni.showToast({
						// 	title: '暂无数据',
						// 	icon: 'none'
						// })
					}
					uni.hideNavigationBarLoading() //关闭加载动画
				})
			},
			goStudy(item) {
				this.courseId = item.id
				if (item.status == 2) request_integral('course', item.id)
				uni.navigateTo({
					url: 'detail?id=' + item.id + '&imgUrl=' + item.imgUrl
				})
			},
			allCourse() {
				uni.navigateTo({
					url: '/pages/allcourse/allcourse'
				})
			},
			goSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style>
	.container-tips {
		min-height: 50vh;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32upx;
		color: #fff;
	}

	.back {
		width: 50upx;
		height: 50upx;
		background: rgba(0, 0, 0, 0.5);
		position: fixed;
		top: 20upx;
		left: 30upx;
		z-index: 1000;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
		background-color: #ffffff;
	}

	.top_view {
		height: var(--status-bar-height);
		width: 100%;
		background-color: #ffffff;
		position: fixed;
		top: 0;
		z-index: 999;
	}

	.fixed-box {
		width: 100%;
		position: fixed;
		/* top: var(--status-bar-height); */
		background: #ffffff;
		z-index: 10;
	}

	.tab-bar {
		height: 80upx;
		line-height: 80upx;
		color: #000000;
		font-size: 32upx;
		width: 100%;
		background: #ffffff;
	}

	.tab {
		width: 750upx;
		overflow: hidden;
		/* position: fixed; */
	}

	.train-info-box {
		width: 650upx;
		border-radius: 40upx;
		box-shadow: 10upx 6upx 30upx #e2e8ec;
		margin-top: 30upx;
		margin-left: 30upx;
		padding: 20upx;
		display: flex;
		position: relative;
	}

	.course-info-box {
		flex: 1;
		padding: 20upx;
		margin-bottom: 20upx;
		background: #ffffff;
		border-radius: 20upx;
	}

	.course-video-box {}

	.info-img {
		width: 350upx;
		height: 180upx;
		border-radius: 20upx;
	}

	.top-type {
		width: 100%;
	}

	.info-title {
		flex: 1;
		font-size: 28upx;
		font-weight: bold;
		color: #333333;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		line-height: 40upx;
	}

	.info-brief {
		font-size: 22upx;
		color: #666666;
		line-height: 30upx;
		height: 60upx;
		margin-top: 10upx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.info-tips {
		font-size: 24upx;
		font-weight: 400;
		color: #848484;
		/* margin-top: 30upx; */
		display: flex;
		align-items: center;
	}
</style>
